package com.zzyl.nursing.task;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.service.IDeviceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component("deviceDataTask")
public class DeviceDataTask {

    @Autowired
    private IDeviceDataService deviceDataService;

    public void deleteExpiredData() {
        // 查找 15 天前的数据
        // 获取当前时间
        Date now = DateUtils.getNowDate();
        // 计算15天前的日期
        Date date = DateUtils.addDays(now, -15);
        // 查找15天前的数据
        List<DeviceData> dataList = deviceDataService.lambdaQuery()
                .lt(DeviceData::getAlarmTime, date).list();
        // 收集删除的 id 集合
        List<Long> ids = dataList.stream().map(DeviceData::getId).collect(Collectors.toList());
        // 执行删除操作
        if (!ids.isEmpty()) {
            deviceDataService.removeBatchByIds(ids);
        }
    }

}
